<template>
  <el-container class="home-container">
    <el-container>
      <el-main>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
                type="primary"
                size="mini"
                @click="handleAdd"
            >新增问题分类</el-button>
            <span style="margin-left: 1750px;" @click="questionLists">返回</span>
          </el-col>
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="this.questionList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="ID" align="center" prop="id">
            <template slot-scope="scope">
              <span @click="questionKindById(scope.row)" style="color: #1890ff;">{{scope.row.id}}</span>
            </template>
          </el-table-column>
          <el-table-column label="问题中文名" align="center" prop="questionType" />
          <el-table-column label="问题英文名" align="center" prop="questionEnglishType" />
          <el-table-column label="客户端" align="center" prop="client" >
            <template slot-scope="scope">
              <span v-if="scope.row.client==1">快递员</span>
              <span v-if="scope.row.client==2">客户端</span>
              <span v-if="scope.row.client==3">智能终端</span>
            </template>
          </el-table-column>
          <el-table-column label="排序号" align="center" prop="sort" />
          <el-table-column label="创建时间" align="center" prop="createDate" width="180">
            <template slot-scope="scope">
              {{scope.row.createDate|format('yyyy-MM-dd hh:mm:ss')}}
            </template>
          </el-table-column>
          <el-table-column label="状态" align="center" prop="status" >
          <template slot-scope="scope">
            <span v-if="scope.row.status==1">正常</span>
            <span v-if="scope.row.status==2">禁用</span>
          </template>
          </el-table-column>
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button
                  size="mini"
                  type="text"
                  @click="deleteQuestionKind(scope.row)"
              >删除</el-button>
                <el-button
                    size="mini"
                    type="text"
                    @click="updateStatusFalse(scope.row.id)"
                    v-if="scope.row.status==1"
                >禁用</el-button>
                <el-button
                    size="mini"
                    type="text"
                    v-if="scope.row.status==2"
                    @click="updateStatusTrue(scope.row.id)"
                >启用</el-button>
            </template>
          </el-table-column>
        </el-table>
        <div>
          <el-pagination
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              :current-page="pageNumber"
              :page-sizes="[ 4, 6, 8]"
              :page-size=pageSize
              layout="total, sizes, prev, pager, next, jumper"
              :total="total">
          </el-pagination>
        </div>
        <!-- 添加 -->
        <el-dialog title="添加分类" :visible.sync="open" width="500px" append-to-body>
          <el-form ref="form" :model="form" :rules="rules" label-width="120px">
            <el-form-item label="分类中文名称" prop="questionType" required>
              <el-input v-model="form.questionType" placeholder="分类中文名称" />
            </el-form-item>
            <el-form-item label="分类英文名称" prop="questionEnglishType" required>
              <el-input v-model="form.questionEnglishType" placeholder="Account problem" />
            </el-form-item>
            <el-form-item label="客户端" prop="client">
              <el-radio v-model="form.client" label="1">快递员</el-radio>
              <el-radio v-model="form.client" label="2">客户端</el-radio>
            </el-form-item>
            <el-form-item label="排序号" prop="sort">
              <el-input v-model="form.sort" placeholder="数字越小越靠前" />
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button type="primary" @click="submitForm">确 定</el-button>
            <el-button @click="cancel">取 消</el-button>
          </div>
        </el-dialog>
        <!-- 编辑 -->
        <el-dialog title="编辑分类" :visible.sync="open1" width="500px" append-to-body>
          <el-form ref="form" :model="form" :rules="rules" label-width="120px">
            <el-form-item label="ID" prop="id">
             {{form.id}}
            </el-form-item>
            <el-form-item label="分类中文名称" prop="questionType">
              <el-input v-model="form.questionType" placeholder="分类中文名称" />
            </el-form-item>
            <el-form-item label="分类英文名称" prop="questionEnglishType">
              <el-input v-model="form.questionEnglishType" placeholder="Account problem" />
            </el-form-item>
            <el-form-item label="客户端" prop="client">
              <el-radio v-model="form.client" :label="1">快递员</el-radio>
              <el-radio v-model="form.client" :label="2">客户端</el-radio>
            </el-form-item>
            <el-form-item label="排序号" prop="sort">
              <el-input v-model="form.sort" placeholder="数字越小越靠前" />
            </el-form-item>
            <el-form-item label="状态" prop="status">
             {{form.status==1?'正常':'禁用'}}
            </el-form-item>
            <el-form-item label="引用" prop="quote">
              {{form.quote==1?'已被引用':'未引用'}}
            </el-form-item>
            <el-form-item label="创建时间" prop="createDate">
                {{form.createDate|format('yyyy-MM-dd hh:mm:ss')}}
            </el-form-item>
            <el-form-item label="最后修改时间" prop="lastDate">
                {{form.lastDate|format('yyyy-MM-dd hh:mm:ss')}}
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button type="primary" @click="submitForm1">确 定</el-button>
            <el-button @click="cancel">取 消</el-button>
          </div>
        </el-dialog>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
import axios from "axios";
export default {
  data() {
    return {
      pageNumber: 1,
      pageSize: 4,
      total: 0,
      // idd:'',
      // phone:'',
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      // 快递员列表表格数据
      questionList: [{

      }],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      open1: false,
      // 表单参数
      form: {
        id:'',
      },
      // 表单校验
      rules: {
        isDel: [
          { required: true, message: "逻辑删除 1否 2是  不能为空", trigger: "blur" }
        ],
      },
      activeIndex1: '4-2',
      activeIndex: '6',
      name: sessionStorage.getItem("name"),
    };
  },
  created() {
    this.getList();
  },

  methods: {
    tiaozhuan(){
      this.$router.push({
        path:'/zhanghuPage'
      })
    },
    baoguo(){
      this.$router.push(
          {path: "/OutPackage"}
      )
    },
    goToMachine(){
      this.$router.push(
          {path: "/machineHome"}
      )
    },
    getList() {
      axios.get('http://localhost:10021/question/questionKindList?pageNum='+this.pageNumber+'&pageSize='+this.pageSize).then(response =>{
        this.questionList = response.data.list;
        this.total=response.data.total;
        this.loading = false;
      })
    },
    questionLists() {
      this.$router.push(
          {path: "/questionList"}
      )
    },
    // 分页
    handleSizeChange(val) {
      this.pageSize=val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.pageNumber=val;
      this.getList();
    },

    // 详情
    questionKindById(row){
      this.form.id=row.id;
      this.form=row;
      this.open1 = true;
    },
    //添加确定按钮
    submitForm(){
      axios.post("http://localhost:10021/question/questionKindAdd",this.form).then(response =>{
        this.form = response.data;
        this.$message("新增成功");
        this.open = false;
        this.getList();
      })
    },
    //删除分类
    deleteQuestionKind(form){
      if(form.quote==1){
        this.$message("已经被引用无法删除！");
        return 0;
      }else{
        axios.get("http://localhost:10021/question/deleteQuestionKind?id="+form.id).then(response =>{
          this.form = response.data;
          this.$message("删除成功");
          this.getList();
        })
      }

    },
    /** 编辑按钮 */
    submitForm1() {
            axios.post("http://localhost:10021/question/questionKindUpdate",this.form).then(response =>{
              this.form = response.data;
              this.$message("编辑成功");
              this.open1 = false;
              this.getList();
            })
    },
    // 状态变更
    updateStatusFalse(id){
      this.$confirm('确定要禁用？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        axios.put("http://localhost:10021/question/updatKindFalse?id="+id).then(response =>{
          this.form = response.data;
          this.getList();
        })
        this.$message({
          type: 'success',
          message: '成功'
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消'
        });
      });
    },
    // 状态变更
    updateStatusTrue(id){
      this.$confirm('确定要启用？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        axios.put("http://localhost:10021/question/updateKindTrue?id="+id).then(response =>{
          this.form = response.data;
          this.getList();
        });
        this.$message({
          type: 'success',
          message: '成功'
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消'
        });
      });
    },
    xitong(){
      this.$router.push(
          {path: "/OutSystems"}
      )
    },
    notice() {
      this.$router.push(
          {path: "/HomePage"}
      )
    },
    //跳转其他页面
    other() {
      this.$router.push(
          {path: "/otherView"}
      )
    },
    // 页面跳转
    expressList(){
      this.$router.push(
          {path: "/expressList"}
      )
    },
    shouye(){
      this.$router.push(
          {path: "/"}
      )
    },
    handleSelect(key) {
      console.log(key);
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.open1 = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        questionType: null,
        questionEnglishType: null,
        client: null,
        sort: null,
      };
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加快递员列表";
    },
  },
  filters:{
    format(value,arg){
      function dateFormat(date, format) {
        if (typeof date === "string") {
          var mts = date.match(/(\/Date\((\d+)\)\/)/);
          if (mts && mts.length >= 3) {
            date = parseInt(mts[2]);
          }
        }
        date = new Date(date);
        if (!date || date.toUTCString() == "Invalid Date") {
          return "";
        }
        var map = {
          "M": date.getMonth() + 1, //月份
          "d": date.getDate(), //日
          "h": date.getHours(), //小时
          "m": date.getMinutes(), //分
          "s": date.getSeconds(), //秒
          "q": Math.floor((date.getMonth() + 3) / 3), //季度
          "S": date.getMilliseconds() //毫秒
        };
        format = format.replace(/([yMdhmsqS])+/g, function (all, t) {
          var v = map[t];
          if (v !== undefined) {
            if (all.length > 1) {
              v = '0' + v;
              v = v.substr(v.length - 2);
            }
            return v;
          } else if (t === 'y') {
            return (date.getFullYear() + '').substr(4 - all.length);
          }
          return all;
        });
        return format;
      }

      return dateFormat(value,arg);

    }
  },
};
</script>

<style lang="less">
#app {
  margin-top: 0;
}

.home-container {
  height: 100%;
}

.el-header {
  background-color: #545c64;
  display: flex; //设置显示为flex布局
  justify-content: space-between; //设置为flex左右布局
  padding-left: 0; //左内边距为0（Logo贴左边）
  align-items: center; //元素上下居中（防止右边按钮贴上下边）
  color: #fff;
  font-size: 18px;

  > div {
    //内嵌的div样式
    display: flex;
    align-items: center; //Logo和文字上下居中
    #fontLogo {
      width: 200px;
      margin-left: 10px; //文字左侧设置间距，防止与Logo紧贴
    }
    #fontName {
      width: 100px;
      margin-right: 10px;
    }
  }
}

#indexMenu {
  width: 100%;
}

.el-menu {
  background-color: #545c64;
}

.el-aside {
  background-color: #545c64;
  scrollbar-width: none; /* firefox */
  -ms-overflow-style: none; /* IE 10+ */
  overflow-x: hidden;
  overflow-y: auto;
}

.el-main {
  background-color: #eaedf1;
  height: 1000px;
}
</style>

